package com.sky.loveshop.utils;

import com.sky.loveshop.api.Apis;
import com.sky.loveshop.api.QiYeWeChatRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author xcy
 * @version V1.0
 * @description 企业微信发送消息工具类
 * @date 2025/01/12 18:13
 */
@Slf4j
@Component
public class QiYeWeChatSendMsgUtil {

    /**
     * 发送文本类型消息
     *
     * @param key  企业微信机器人key
     * @param text 文本内容
     */
    public void sendTextMsg(String key, String text) {
        QiYeWeChatRequest.MessageRequest request = new QiYeWeChatRequest.MessageRequest();
        request.setMsgtype("text");
        QiYeWeChatRequest.ContentText contentText = new QiYeWeChatRequest.ContentText();
        contentText.setContent(text);
        request.setText(contentText);
        try {
            QiYeWeChatRequest.MessageResponse response = Apis.QI_YE_WE_CHAT_API.webhookSend(key, request);
            log.info("sendTextMsg response={}, key={}, text={}", response, key, text);
        } catch (Exception e) {
            log.error("sendTextMsg error key={}, text={}", key, text, e);
        }
    }

    /**
     * 发送markdown类型消息
     *
     * @param key      企业微信机器人key
     * @param markdown markdown内容
     */
    public void sendMarkdownMsg(String key, String markdown) {
        QiYeWeChatRequest.MessageRequest request = new QiYeWeChatRequest.MessageRequest();
        request.setMsgtype("markdown");
        QiYeWeChatRequest.ContentMarkdown contentMarkdown = new QiYeWeChatRequest.ContentMarkdown();
        contentMarkdown.setContent(markdown);
        request.setMarkdown(contentMarkdown);
        try {
            QiYeWeChatRequest.MessageResponse response = Apis.QI_YE_WE_CHAT_API.webhookSend(key, request);
            log.info("sendMarkdownMsg response={}, key={}, text={}", response, key, markdown);
        } catch (Exception e) {
            log.error("sendMarkdownMsg error key={}, text={}", key, markdown, e);
        }
    }
}
